<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Connection
 *
 * @author Ramiro
 */
class ConnectionSecure {

    static private $instancia = NULL;
    private $servidor;
    private $usuario;
    private $password;
    private $basedatos;
    private $conexion;

    private function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }

    public function __construct() {
        $config = Conf::getInstancia();
        $this->servidor = $config->getHOST();
        $this->usuario = $config->getUSER();
        $this->password = $config->getPASSWORD();
        $this->basedatos = $config->getDATABASE();
    }

    //Función de conexión
    public function conectar() {
        $this->conexion = new mysqli($this->servidor, $this->usuario, $this->password,$this->basedatos) or DIE(mysql_error());
        
    }

    
    public function getConexion() {
        return $this->conexion;
    }

    

        public function desconectar() {
         $this->conexion-> close();
    }

    public function query($query) {
        $this->conectar();
        $res = $this->conexion->query($query) or die(mysql_error());
        $this->desconectar();
        return $res;
    }

    public function obtenerQuery($query) {
        $this->conectar();
        $res = mysql_query($query) or die(mysql_error());
        $this->desconectar();
        return $res;
    }
    
    public function ejecutarPrepared($query) {
        $this->conectar();
        $res = mysql_query($query) or die(mysql_error());
        $this->desconectar();
        return $res;
    }

    public function obtenerQuerySinCerrarConnection($query) {
        $this->conectar();
        $res = mysql_query($query) or die(mysql_error());
        return $res;
    }

    public function obtenerQuerySinAbrirConnection($query) {

        $res = mysql_query($query) or die(mysql_error());
        $this->desconectar();
        return $res;
    }

    public function obtenerQuerySinAbrirCerrar($query) {
        $res = mysql_query($query) or die(mysql_error());
        return $res;
    }

    static public function getInstancia() {
        if (self::$instancia == NULL) {
            self::$instancia = new ConnectionSecure();
        }
        return self::$instancia;
    }

    public function mysql_escape_string($var) {
        $this->conectar();
        $result = mysql_escape_string($var);
        $this->desconectar();
        return $result;
    }

}

?>
